﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Framework.Common;
using DomPrint.Common;
using Model.Custome;

namespace DomPrint.UControl
{
    public partial class UCDom : UserControl, IDomPrint
    {
        #region 接口实现
        /// <summary>
        /// 自动添加
        /// </summary>
        /// <param name="dataType">数据类型</param>
        /// <param name="startNum">起始编号</param>
        /// <param name="endNum">终止编号</param>
        public void IAutoAdd(string dataType, string startNum, string endNum)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 手动单个添加
        /// </summary>
        /// <param name="startNum">起始编号</param>
        /// <param name="endNum">终止编号</param>  
        public void IManAddSingle(string startNum, string endNum)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 手动批量添加
        /// </summary>
        /// <param name="startNum">起始编号</param>
        /// <param name="endNum">终止编号</param>   
        public void IManAddBatch(string startNum, string endNum)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 删除编号列表
        /// </summary>
        public void IDelete()
        {
            foreach (ListViewItem item in lvList.SelectedItems)
            {
                lvList.Items.Remove(item);
            }
        }
        /// <summary>
        /// 清空编号列表
        /// </summary>
        public void IClear()
        {
            lvList.Items.Clear();
        }
        #endregion

        #region 属性
        /// <summary>
        /// 介质类型
        /// </summary>
        public MediumType TypeName
        { get; set; }
        /// <summary>
        /// 硬盘类型
        /// </summary>
        public HDType hdType
        {
            get
            {
                if (OptHS.Checked)
                    return HDType.HS;
                else
                    return HDType.DOM;
            }
        }

        /// <summary>
        /// 编号列表集合
        /// </summary>
        public List<ListItemInfo> ListItem
        {
            get
            {
                List<ListItemInfo> list = new List<ListItemInfo>();
                switch (TypeName)
                {
                    case MediumType.Tape:
                        foreach (ListViewItem item in lvList.Items)
                        {
                            ListItemInfo itemInfo = new ListItemInfo
                            {
                                CDTXM = item.SubItems[0].Text,//磁带条形码
                                CDBH = item.SubItems[1].Text,//磁带编号
                                SJLX = item.SubItems[2].Text,//数据类型
                                BYRQ = item.SubItems[3].Text,//备份日期
                                JZLSH = item.SubItems[4].Text//介质流水号
                            };
                            list.Add(itemInfo);
                        }
                        break;
                    default:
                        foreach (ListViewItem item in lvList.Items)
                        {
                            ListItemInfo itemInfo = new ListItemInfo
                            {
                                JZBH = item.SubItems[0].Text,//介质编号
                                TGDW = item.SubItems[1].Text,//提供单位
                                ZLMC = item.SubItems[2].Text,//资料名称
                                TGRQ = item.SubItems[3].Text,//提供日期
                                JZLSH = item.SubItems[4].Text//介质流水号
                            };
                            list.Add(itemInfo);
                        }
                        break;
                }

                return list;
            }
        }
        /// <summary>
        /// 磁带类型
        /// </summary>
        public TapeType tapeType
        {
            get
            {
                if (OptSLR.Checked)
                    return TapeType.SLR;
                else
                    return TapeType.Tape9840;
            }
        }
        #endregion
        public UCDom()
        {
            InitializeComponent();
        }
        public UCDom(MediumType type)
        {
            InitializeComponent();
            DataBaind(type);
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="type"></param>
        public void DataBaind(MediumType type)
        {
            try
            {
                TypeName = type;
                DataTable dt;
                switch (type)
                {
                    case MediumType.HD:
                        #region HD
                        pHD.Visible = true;
                        SetLocation();
                        dt = DataAccess.MEDIUM_HD_T.GetList(" MEDIUM_SYZT=0 order by MEDIUM_ID").Tables[0];
                        BindCombo(dt, "MEDIUM_BH");
                        #endregion
                        break;
                    case MediumType.CD:
                        #region CD
                        ComboDataType.Items.Add("DOM正射影像五万生产");
                        ComboDataType.Items.Add("DOM正射影像五万收集");
                        ComboDataType.Items.Add("DOM正射影像扫描数据");
                        ComboDataType.Items.Add("DOM正射影像土地覆盖数据");
                        ComboDataType.Items.Add("长城测量");
                        ComboDataType.Items.Add("原始卫星影像");
                        ComboDataType.Items.Add("成果数据");
                        ComboDataType.Items.Add("历史航片数据");
                        ComboDataType.Items.Add("移动通讯数据");
                        ComboDataType.Items.Add("对外提供数据");
                        ComboDataType.Items.Add("其它数据");
                        ComboDataType.Text = "DOM正射影像五万生产";

                        ComboDataType_SelectedIndexChanged(ComboDataType, null);
                        dt = DataAccess.MEDIUM_CD_T.GetList(" MEDIUM_SYZT=0 order by MEDIUM_ID").Tables[0];
                        BindCombo(dt, "MEDIUM_BH");
                        #endregion
                        break;
                    case MediumType.DVD:
                        #region DVD
                        SetLocation();
                        dt = DataAccess.MEDIUM_DVD_T.GetList(" MEDIUM_SYZT=0 order by MEDIUM_ID").Tables[0];
                        BindCombo(dt, "MEDIUM_BH");
                        #endregion
                        break;
                    case MediumType.Tape:
                        #region Tape
                        PTape.Visible = true;
                        SetLocation();
                        lvList.Columns[0].Text = "磁带条形码";
                        lvList.Columns[1].Text = "磁带编号";
                        lvList.Columns[2].Text = "数据类型";
                        lvList.Columns[3].Text = "备份日期";
                        dt = DataAccess.MEDIUM_TP_T.GetList(" MEDIUM_SYZT=0 order by MEDIUM_ID").Tables[0];
                        BindCombo(dt, "MEDIUM_BH");
                        #endregion
                        break;
                    case MediumType.Other:
                        #region Other
                        dt = DataAccess.MEDIUM_QT_T.GetList(" MEDIUM_SYZT=0 order by MEDIUM_ID").Tables[0];
                        BindCombo(dt, "MEDIUM_BH");
                        #endregion
                        SetLocation();
                        break;
                }
            }
            catch
            {
            }
        }
        public void BindCombo(DataTable dt, string columnName)
        {
            ComboStart.DataSource = dt;
            ComboStart.DisplayMember = columnName;
            ComboStart.ValueMember = columnName;
            DataTable dt1 = dt.Copy();
            ComboEnd.DataSource = dt1;
            ComboEnd.DisplayMember = columnName;
            ComboEnd.ValueMember = columnName;
        }
        /// <summary>
        /// 设置元素显示
        /// </summary>
        private void SetLocation()
        {
            gbAuto.Size = new Size(236, 99);
            lbStart.Location = new Point(6, 23);
            lbEnd.Location = new Point(7, 49);
            ComboStart.Location = new Point(72, 20);
            ComboEnd.Location = new Point(72, 46);
            lbDataType.Visible = false;
            ComboDataType.Visible = false;
            btnAutoAdd.Location = new Point(155, 72);
        }

        private void btnManSing_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否为标签添加注释?", "提示", MessageBoxButtons.OKCancel);
            string Annotation1 = "", Annotation2 = "", Annotation3 = "";

            if (dr == DialogResult.OK)//如果点击“确定”按钮
            {
                InputAnnotation inputAnn = new InputAnnotation();
                inputAnn.ShowDialog();
                Annotation1 = inputAnn.Annotation1;
                Annotation2 = inputAnn.Annotation2;
                Annotation3 = inputAnn.Annotation3;
                inputAnn.Dispose();
            }

            ListViewItem LtItm = new ListViewItem(new string[] { txtManStart.Text.Trim(), Annotation1, Annotation2, Annotation3, "" });
            lvList.Items.Add(LtItm);
        }

        private void txtManStart_TextChanged(object sender, EventArgs e)
        {
            if (txtManStart.Text.Trim() != "")
            {
                btnManSing.Enabled = true;
                btnManBatch.Enabled = true;
            }
            else
            {
                btnManSing.Enabled = false;
                btnManBatch.Enabled = false;
            }
        }

        private void btnManBatch_Click(object sender, EventArgs e)
        {
            try
            {
                string str1 = "", str2, str3, str;
                int strlen = 0;
                if (txtManStart.Text.Trim().Length != txtManEnd.Text.Trim().Length)
                    DomCommon.ShowMsg("输入的编号长度不相等，请重新输入！");
                else
                    str1 = txtManStart.Text.Trim();
                str2 = txtManEnd.Text.Trim();
                int lenCount;
                lenCount = str1.Length;
                str3 = "";
                for (int i = 0; i < lenCount; i++)
                {
                    string s1, s2;
                    s1 = str1.Substring(i, 1);
                    s2 = str2.Substring(i, 1);
                    if (s1 == s2)
                        str3 = str3 + s1;//存储2个字符串中相同的部分
                    else
                    {
                        str1 = str1.Substring(i, str1.Length - i);
                        str2 = str2.Substring(i, str2.Length - i);
                        strlen = str1.Length; // 不相等部分的长度
                        break;
                    }
                }
                long number1, number2;
                number1 = int.Parse(str1);
                number2 = int.Parse(str2);
                if (number1 > number2)
                    DomCommon.ShowMsg("终止编号应大于起始编号，请重新输入！");
                else
                {
                    DialogResult s;
                    string txtStr1, txtStr2, txtStr3;
                    txtStr1 = "";
                    txtStr2 = "";
                    txtStr3 = "";
                    s = DomCommon.ShowAsk("是否为标签添加注释?");
                    if (s == DialogResult.Yes)//如果点击“确定”按钮
                    {
                        InputAnnotation inputAnn = new InputAnnotation();
                        inputAnn.ShowDialog();
                        txtStr1 = inputAnn.Annotation1;//提供单位
                        txtStr2 = inputAnn.Annotation2;//资料名称
                        txtStr3 = inputAnn.Annotation3;//提供日期
                        inputAnn.Dispose();
                    }

                    for (int j = 0; j <= number2 - number1; j++)
                    {
                        int k = strlen - (number1 + j).ToString().Length;
                        str = str3 + "0".GetNumString(k) + (number1 + j);
                        ListViewItem LtItm = new ListViewItem(new string[] { str, txtStr1, txtStr2, txtStr3, "" });
                        lvList.Items.Add(LtItm);
                    }
                }
            }
            catch
            {
                DomCommon.ShowMsg("编号输入有误,请重新输入!");
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            IDelete();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            IClear();
        }

        private void UCDom_Load(object sender, EventArgs e)
        {

        }

        private void btnAutoAdd_Click(object sender, EventArgs e)
        {
            string strStart = ComboStart.Text;
            string strEnd = ComboEnd.Text;
            if (string.IsNullOrEmpty(strStart) || string.IsNullOrEmpty(strEnd))
            {
                DomCommon.ShowMsg("请选择硬盘起始编号和终止编号!");
                return;
            }

            int j = 0, count = 0;
            string str;
            for (int i = 0; i < ComboStart.Items.Count; i++)
            {
                if (((System.Data.DataRowView)(ComboStart.Items[i])).Row["MEDIUM_BH"].ToString() == strStart)
                    j = i;
                if (((System.Data.DataRowView)(ComboEnd.Items[i])).Row["MEDIUM_BH"].ToString() == strEnd)
                    count = i;
            }
            if (j > count)
            {
                DomCommon.ShowMsg("终止编号选择错误!");
                return;
            }
            string where = "";
            for (int k = j; k <= count; k++)
            {
                str = ((System.Data.DataRowView)(ComboStart.Items[k])).Row["MEDIUM_BH"].ToString();
                where += string.Format("'{0}',", str);
            }
            where = where.Substring(0, where.Length - 1);
            DataTable dt;
            switch (TypeName)
            {
                case MediumType.HD:
                    #region HD
                    dt = DataAccess.MEDIUM_HD_T.GetList(" MEDIUM_BH in(" + where + ")" + " order by MEDIUM_ID").Tables[0];
                    SetAutoCombo(dt);
                    #endregion
                    break;
                case MediumType.Tape:
                    #region Type
                    dt = DataAccess.MEDIUM_TP_T.GetList(" MEDIUM_BH in(" + where + ")" + " order by MEDIUM_ID").Tables[0];
                    foreach (DataRow item in dt.Rows)
                    {
                        ListViewItem LtItm = new ListViewItem();
                        LtItm = new ListViewItem(new string[] { 
                                    item["MEDIUM_BH"].ToString(),
                                    item["MEDIUM_SN"].ToString(),//磁带的原编号
                                    item["MEDIUM_ZLLX"].ToString(), //数据类型
                                    DateTime.Parse(item["MEDIUM_USERQ"].ToString()).ToString("yyyy-MM-dd"), //备份日期
                                    item["MEDIUM_LSH"].ToString()//流水号
                                                                        });
                        lvList.Items.Add(LtItm);
                    }
                    #endregion
                    break;
                case MediumType.CD:
                    #region CD
                    dt = DataAccess.MEDIUM_CD_T.GetList(" MEDIUM_BH in(" + where + ")" + " order by MEDIUM_BH").Tables[0];
                    SetAutoCombo(dt);
                    #endregion
                    break;
                case MediumType.DVD:
                    dt = DataAccess.MEDIUM_DVD_T.GetList(" MEDIUM_BH in(" + where + ")" + " order by MEDIUM_BH").Tables[0];
                    SetAutoCombo(dt);
                    break;
                case MediumType.Other:
                    dt = DataAccess.MEDIUM_QT_T.GetList(" MEDIUM_BH in(" + where + ")" + " order by MEDIUM_BH").Tables[0];
                    SetAutoCombo(dt);
                    break;
                default:
                    break;
            }

        }
        /// <summary>
        /// 绑定自动输入时的开始和结束编号
        /// </summary>
        /// <param name="dt"></param>
        public void SetAutoCombo(DataTable dt)
        {
            foreach (DataRow item in dt.Rows)
            {
                ListViewItem LtItm = new ListViewItem();
                switch (item["MEDIUM_YT"].ToString())
                {
                    case "1"://用于接收
                        if (item["MEDIUM_LY"] == "航摄单位移交")
                            LtItm = new ListViewItem(new string[] { 
                                        item["MEDIUM_BH"].ToString(),
                                        item["MEDIUM_YHTGDW"].ToString(),//提供单位
                                        item["MEDIUM_ZLMC"].ToString(), //资料名称
                                        DateTime.Parse(item["MEDIUM_YHTGRQ"].ToString()).ToString("yyyy-MM-dd"), //提供日期
                                        item["MEDIUM_LSH"].ToString()//流水号
                                        });
                        else
                            LtItm = new ListViewItem(new string[] { 
                                        item["MEDIUM_BH"].ToString(),
                                        item["MEDIUM_YHTGDW"].ToString(),//提供单位
                                        item["MEDIUM_ZLMC"].ToString(), //资料名称
                                        DateTime.Parse(item["MEDIUM_YHTGRQ"].ToString()).ToString("yyyy-MM-dd"), //提供日期
                                        item["MEDIUM_LSH"].ToString()//流水号
                                        });
                        break;
                    case "2"://用于提供
                        LtItm = new ListViewItem(new string[] {
                                    item["MEDIUM_BH"].ToString(),
                                    item["MEDIUM_SYDW"].ToString(),//使用单位
                                    item["MEDIUM_ZLMC"].ToString(), //资料名称
                                    DateTime.Parse(item["MEDIUM_USERQ"].ToString()).ToString("yyyy-MM-dd"), //提供日期
                                    item["MEDIUM_LSH"].ToString()//流水号
                                    });
                        break;
                    case "3"://用于备份
                        LtItm = new ListViewItem(new string[] { 
                                    item["MEDIUM_BH"].ToString(),
                                    item["MEDIUM_DJXH"].ToString(),//登记序号
                                    item["MEDIUM_ZLMC"].ToString(), //资料名称
                                    DateTime.Parse(item["MEDIUM_USERQ"].ToString()).ToString("yyyy-MM-dd"), //备份日期
                                    item["MEDIUM_LSH"].ToString()//流水号
                                                                        });
                        break;
                }
                lvList.Items.Add(LtItm);
            }
        }

        private void ComboDataType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strDataType = ComboDataType.Text;//数据类型
            string strMedium_bh = "";
            switch (strDataType)
            {
                case "DOM正射影像五万生产":
                    strMedium_bh = "CHD";
                    break;
                case "DOM正射影像五万收集": strMedium_bh = "CDJ";
                    break;
                case "DOM正射影像扫描数据": strMedium_bh = "CSM";
                    break;
                case "DOM正射影像土地覆盖数据": strMedium_bh = "CTD";
                    break;
                case "长城测量": strMedium_bh = "CGW";
                    break;
                case "原始卫星影像": strMedium_bh = "CD2";
                    break;
                case "成果数据": strMedium_bh = "CDOM";
                    break;
                case "历史航片数据": strMedium_bh = "CLSHP";
                    break;
                case "移动通讯数据": strMedium_bh = "CYD";
                    break;
                case "对外提供数据": strMedium_bh = "CRS";
                    break;
                case "其它数据": strMedium_bh = "CQT";
                    break;
            }

            ComboStart.Text = "";
            ComboEnd.Text = "";
            DataTable dt = DataAccess.MEDIUM_CD_T.GetList(" MEDIUM_SYZT=0 and MEDIUM_BH like '" + strMedium_bh + "%'  order by MEDIUM_ID").Tables[0];
            DataTable dt1 = dt.Copy();
            ComboStart.DataSource = dt;
            ComboEnd.DataSource = dt1;
            ComboStart.DisplayMember = "MEDIUM_BH";
            ComboStart.ValueMember = "MEDIUM_BH";
            ComboEnd.DisplayMember = "MEDIUM_BH";
            ComboEnd.ValueMember = "MEDIUM_BH";
        }
    }
}
